/**
 * 绑定多个 vao 示例 
 */
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdbool.h>

void monitor(GLFWwindow* win, int W, int H) {
	glViewport(0, 0, W, H);
	return;
}
// 输入监听
void Input(GLFWwindow* window) {

	// 如果按下ESC按键,如果按下返回GLFW_PRESS  如果没有按下返回GLFW_RELEASE
	if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
		// 关闭窗口
		glfwSetWindowShouldClose(window, true);
	}
	else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
		// 使用线框模式绘制
		// 参数1 对所有图形使用
		// 参数2 使用线条绘制
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}
	else {
		// 使用线框模式绘制
		// 参数1 对所有图形使用
		// 参数2 使用填充绘制
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}
}
int main() {

	glfwInit();
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

	GLFWwindow* win = glfwCreateWindow(800, 600, "多个三角形", NULL, NULL);
	if (win == NULL) {
		printf("创建窗口失败\n");
		exit(-1);
	}
	// 创建上下文
	glfwMakeContextCurrent(win);

	// 加载opengl函数指针
	if (gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) == 0) {
		printf("初始化glad失败\n");
		exit(-1);
	}
	// 设置视口大小
	glViewport(0, 0, 800, 600);

	// 注册监听
	glfwSetFramebufferSizeCallback(win, monitor);


	// 着色器
	const char* vertex =
		"#version 330 core\n"
		"layout (location = 0) in vec3 apos;\n"
		"void main(){"
		"	gl_Position = vec4(apos.x,apos.y,apos.z,1.0);"
		"}";
	const char* fragment1 =
		"#version 330 core\n"
		"out vec4 FragColor;\n"
		"void main(){"
		"	FragColor = vec4(0.58f,0.3f,0.75f,1.0f);"
		"}";
	const char* fragment2 =
		"#version 330 core\n"
		"out vec4 FragColor;\n"
		"void main(){"
		"	FragColor = vec4(0.2f,0.4f,0.4f,1.0f);"
		"}";

	unsigned int vertexshader;
	unsigned int fragmentshader1;
	unsigned int fragmentshader2;
	// 创建着色器对象
	vertexshader = glCreateShader(GL_VERTEX_SHADER);
	fragmentshader1 = glCreateShader(GL_FRAGMENT_SHADER);
	fragmentshader2 = glCreateShader(GL_FRAGMENT_SHADER);

	// 附加顶点着色器源码到着色器对象上
	glShaderSource(vertexshader, 1, &vertex, NULL);
	glShaderSource(fragmentshader1, 1, &fragment1, NULL);

	glShaderSource(fragmentshader2, 1, &fragment2, NULL);

	// 编译着色器
	glCompileShader(vertexshader);
	glCompileShader(fragmentshader1);
	glCompileShader(fragmentshader2);

	// 检测着色器编译是否成功

	int success;
	int infoLog[512];
	glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &success);
	if (success == 0) {
		glGetShaderInfoLog(vertexshader, 512, NULL, infoLog);
		printf("顶点着色器编译失败,信息如下:%s\n", infoLog);
		exit(-1);
	}

	glGetShaderiv(fragmentshader1, GL_COMPILE_STATUS, &success);
	if (success == 0) {
		glGetShaderInfoLog(fragmentshader1, 512, NULL, infoLog);
		printf("片段着色器编译失败,信息如下%s\n", infoLog);
		exit(-1);
	}

	glGetShaderiv(fragmentshader2, GL_COMPILE_STATUS, &success);
	if (success == 0) {
		glGetShaderInfoLog(fragmentshader2, 512, NULL, infoLog);
		printf("片段着色器编译失败,信息如下%s\n", infoLog);
		exit(-1);
	}

	// 创建一个着色器程序
	unsigned int shaderprogram1;
	unsigned int shaderprogram2;

	shaderprogram1 = glCreateProgram();

	shaderprogram2 = glCreateProgram();
	// 将编译的着色器附加到着色器程序上
	glAttachShader(shaderprogram1, vertexshader);
	glAttachShader(shaderprogram1, fragmentshader1);

	glLinkProgram(shaderprogram1);

	// 将编译的着色器附加到着色器程序上
	glAttachShader(shaderprogram2, vertexshader);
	glAttachShader(shaderprogram2, fragmentshader2);

	glLinkProgram(shaderprogram2);
	// 获取链接状态
	glGetProgramiv(shaderprogram2, GL_LINK_STATUS, &success);
	if (success == 0) {
		glGetProgramInfoLog(shaderprogram2, 512, NULL, infoLog);
		printf("链接失败,信息如下%s\n");
		exit(-1);
	}
	// 使用着色器程序并删除之前的着色器
	glUseProgram(shaderprogram2);
	glDeleteShader(vertexshader);
	glDeleteShader(fragmentshader2);


	float arr1[] = {
		// 第一个三角形
		0.5f,0.75f,0.0f,
		0.25f,0.25f,0.0f,
		0.75f,0.25f,0.0f,
	};

	float arr2[] = {
		// 第二个三角形
		-0.5f,-0.75f,0.0f,
		-0.25f,-0.25f,0.0f,
		-0.75f,-0.25f,0.0f,
	};

	// 生成VBO对象
	unsigned int VBO1, VBO2;
	glGenBuffers(1, &VBO1);
	glGenBuffers(1, &VBO2);

	// 创建顶点数组对象
	unsigned int VAO1, VAO2;
	glGenVertexArrays(1, &VAO1);
	glGenVertexArrays(1, &VAO2);


	// 绑定VAO
	glBindVertexArray(VAO1);
	// 绑定缓冲到目标缓冲
	glBindBuffer(GL_ARRAY_BUFFER, VBO1);
	// 传递数据到缓冲
	glBufferData(GL_ARRAY_BUFFER, sizeof(arr1), arr1, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)NULL);
	// 启用顶点属性
	glEnableVertexAttribArray(0);



	glBindVertexArray(VAO2);
	glBindBuffer(GL_ARRAY_BUFFER, VBO2);
	glBufferData(GL_ARRAY_BUFFER, sizeof(arr2), arr2, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)NULL);
	// 启用顶点属性
	glEnableVertexAttribArray(0);



	// 绑定buffer
	//glBindBuffer(GL_ARRAY_BUFFER, 0);
	//glBindVertexArray(0);

	while (glfwWindowShouldClose(win) == 0) {

		Input(win);

		glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
		glClear(GL_COLOR_BUFFER_BIT);

		glUseProgram(shaderprogram1);

		glBindVertexArray(VAO1);
		glDrawArrays(GL_TRIANGLES, 0, (sizeof(arr1) / sizeof(arr1[0])) / 3);


		glUseProgram(shaderprogram2);
		glBindVertexArray(VAO2);
		glDrawArrays(GL_TRIANGLES, 0, (sizeof(arr2) / sizeof(arr2[0])) / 3);

		glfwSwapBuffers(win);

		glfwPollEvents();

	}

	glfwTerminate();

	return 0;
}
